#include "../ILAppBase.h"
#include "ILControlCollect.h"

il_control* il_ctrl_create(il_control_type type, ILUINT8* paint_buf, il_rect area,
						   ILINT extra_param_cnts, void* extra_param_p[])
{
	il_control* ctrl_p = NULL;

	switch (type)
	{
	case IL_CTRL_LABEL:
		if(extra_param_cnts == 1)
		{
			ctrl_p = (il_control*)il_ctrl_label_create(paint_buf, area, (ILWSTR)extra_param_p[0]);
		}

		break;

	case IL_CTRL_WAITING:
		if(extra_param_cnts == 1)
		{
			ctrl_p = (il_control*)il_ctrl_waiting_create(paint_buf, area, (il_waiting_type)(ILINT)extra_param_p[0]);
		}

		break;

	case IL_CTRL_IMAGE:
		if(extra_param_cnts == 3)
		{
			ctrl_p = (il_control*)il_ctrl_image_create(paint_buf, area,
				(il_image_src_type)(ILINT)extra_param_p[0], (ILWSTR)extra_param_p[1], (ILINT)extra_param_p[2]);
		}

		break;

	case IL_CTRL_SCROLLTEXT:
		if(extra_param_cnts == 3)
		{
			ctrl_p = (il_control*)il_ctrl_scrolltext_create(paint_buf, area,
				(ILWSTR)extra_param_p[0], (ILUINT)extra_param_p[1], (ILUINT)extra_param_p[2]);
		}

		break;

	case IL_CTRL_HYPERLINK:
		if(extra_param_cnts == 2)
		{
			ctrl_p = (il_control*)il_ctrl_hyperlink_create(paint_buf, area, (ILWSTR)extra_param_p[0], (VMCHAR*)extra_param_p[1]);
		}

		break;

	case IL_CTRL_BUTTON:
		if(extra_param_cnts == 5)
		{
			ctrl_p = (il_control*)il_ctrl_button_create(paint_buf, area,
				(il_button_type)(ILUINT)extra_param_p[0], (ILWSTR)extra_param_p[1], (ILWSTR)extra_param_p[2],
				(ILWSTR)extra_param_p[3], (ILUINT)extra_param_p[4]);
		}

		break;

	case IL_CTRL_TABLEBAR:
		if(extra_param_cnts == 3)
		{
			ctrl_p = (il_control*)il_ctrl_tablebar_create(paint_buf, area,
				(ILINT)extra_param_p[0], (il_tablebar_src_type)(ILUINT)extra_param_p[1], (il_tablebar_show_type)(ILUINT)extra_param_p[2]);
		}

		break;

	case IL_CTRL_LISTITEM:
		if(extra_param_cnts == 1)
		{
			ctrl_p = (il_control*)il_ctrl_listitem_create(paint_buf, area, (il_rect*)extra_param_p[0]);
		}

		break;

	case IL_CTRL_LIST:
		if(extra_param_cnts == 0)
		{
			ctrl_p = (il_control*)il_ctrl_list_create(paint_buf, area);
		}

		break;

	case IL_CTRL_PROGRESSBAR:
		if(extra_param_cnts == 1)
		{
			ctrl_p = (il_control*)il_ctrl_progressbar_create(paint_buf, area, (il_progressbar_type)(ILUINT)extra_param_p[0]);
		}

		break;

	case IL_CTRL_RANKING:
		if(extra_param_cnts == 4)
		{
			ctrl_p = (il_control*)il_ctrl_rank_create(paint_buf, area, (il_image_src_type)(ILUINT)extra_param_p[0],
				(ILWSTR)extra_param_p[1], (ILWSTR)extra_param_p[2], (ILINT)extra_param_p[3]);
		}

		break;

	case IL_CTRL_FILLEDAREA:
		if(extra_param_cnts == 0)
		{
			ctrl_p = (il_control*)il_ctrl_filledarea_create(paint_buf, area);
		}

		break;

	default:
		break;
	}

	return ctrl_p;
}

il_ret il_ctrl_destroy(il_control* ctrl_p)
{
	il_control_type type;
	il_ret ret = IL_OK;

	if(!ctrl_p)
	{
		ret = IL_INVALID_PARAM;
		return ret;
	}

	type = ctrl_p->ctrl_type;

	switch(type)
	{
	case IL_CTRL_LABEL:
		return il_ctrl_label_destroy((il_label*)ctrl_p);

	case IL_CTRL_WAITING:
		return il_ctrl_waiting_destroy((il_waiting*)ctrl_p);

	case IL_CTRL_IMAGE:
		return il_ctrl_image_destroy((il_image*)ctrl_p);

	case IL_CTRL_SCROLLTEXT:
		return il_ctrl_scrolltext_destroy((il_scrolltext*)ctrl_p);

	case IL_CTRL_HYPERLINK:
		return il_ctrl_hyperlink_destroy((il_hyperlink*)ctrl_p);

	case IL_CTRL_BUTTON:
		return il_ctrl_button_destroy((il_button*)ctrl_p);

	case IL_CTRL_TABLEBAR:
		return il_ctrl_tablebar_destroy((il_tablebar*)ctrl_p);

	case IL_CTRL_LISTITEM:
		return il_ctrl_listitem_destroy((il_listitem*)ctrl_p);

	case IL_CTRL_LIST:
		return il_ctrl_list_destroy((il_list*)ctrl_p);

	case IL_CTRL_PROGRESSBAR:
		return il_ctrl_progressbar_destory((il_progressbar*)ctrl_p);

	case IL_CTRL_RANKING:
		return il_ctrl_rank_destroy((il_rank*)ctrl_p);

	case IL_CTRL_FILLEDAREA:
		return il_ctrl_filledarea_destroy((il_filled_area*)ctrl_p);

	default:
		return IL_INVALID_PARAM;
	}
}

static void il_ctrl_move(il_control* ctrl_p, ILINT x_off, ILINT y_off)
{
	if(!ctrl_p)
		return;

	ctrl_p->area.x += (ILINT16)x_off;
	ctrl_p->area.y += (ILINT16)y_off;
}

static void il_ctrl_change_area(il_control* ctrl_p, il_rect area)
{
	if(!ctrl_p)
		return;

	memcpy(&ctrl_p->area, &area, sizeof(il_rect));
}

void il_ctrl_init(il_control* ctrl_p, ILUINT8* paint_buf, il_rect* area)
{
	if(!ctrl_p)
		return;

	memcpy(&ctrl_p->area, area, sizeof(il_rect));
	ctrl_p->paint_buf = paint_buf;
	ctrl_p->change_area_hdlr = il_ctrl_change_area;
	ctrl_p->move = il_ctrl_move;
	ctrl_p->pen_hdlr = NULL;
	ctrl_p->key_hdlr = NULL;

}

void il_ctrl_flush(void)
{
	 il_flush();
}